﻿using System;
using System.Runtime.Serialization;
using UnityEngine;

namespace GMME
{
    [DataContract]
    public class AudioPlayStateInfo
    {
        public HWAudioClipsStateEnum StateEnum { get; set; }

        [DataMember(Name = "state")] public int State { get; set; }

        [DataMember(Name = "soundId")] public int SoundId { get; set; }

        [DataMember(Name = "reason")] public string Reason { get; set; }

#if UNITY_ANDROID
        internal static AudioPlayStateInfo ConvertAudioPlayStateInfo(AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                AudioPlayStateInfo audioPlayStateInfo = new AudioPlayStateInfo();
                audioPlayStateInfo.SoundId = javaObject.Call<int>("getSoundId");
                audioPlayStateInfo.Reason = javaObject.Call<string>("getReason");
                AndroidJavaObject stateObj = javaObject.Call<AndroidJavaObject>("getState");
                int stateInt = stateObj.Call<int>("ordinal");
                Debug.LogFormat("the java enum oridnal is : {0}", stateInt);
                if (Enum.IsDefined(typeof(HWAudioClipsStateEnum), stateInt))
                {
                    audioPlayStateInfo.StateEnum =
                        (HWAudioClipsStateEnum) Enum.ToObject(typeof(HWAudioClipsStateEnum), stateInt);
                }

                return audioPlayStateInfo;
            }

            return null;
        }
#endif
    }
}